package com.GLM.Viewer.Users;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.NavUtils;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.GLM.Constants.Constant;
import com.GLM.Entity.UserInfoDetailEntity;
import com.GLM.Entity.UserInfoEntity;
import com.GLM.Tools.DB.DAO.UserInfoDAO;
import com.GLM.Tools.Utils.ViewsUtil;
import com.GLM.Tools.Utils.Image.AsynImageLoader;
import com.GLM.Tools.Utils.JSON.JSONResponse;
import com.GLM.Tools.Utils.JSON.JSONUtil;
import com.GLM.Tools.Utils.JSON.JSONResponse.Status;
import com.GLM.Viewer.R;
import com.GLM.Viewer.FirstLogin.FirstLoginActivity;
import com.GLM.Viewer.FirstLogin.RegisterSecondViewActivity;
import com.GLM.Viewer.Main.MainActivity;
import com.google.gson.Gson;

public class UserSettingActivity extends Activity {

	private final static int USERINFO_LOADING = 111;
	private final static int USERINFO_LONDED = 222;
	private final static int UPLOADING_INFO = 1;
	private final static int UPLOADED_INFO = 2;
	View mInputFieldOneLine;
	TextView mInputFieldOneLineTextView;
	EditText mInputFieldOneLineEditText;

	View mInputFieldMoreLines;
	EditText mInputFieldMoreLinesEditText;

	Animation mAnimIn;
	Animation mAnimOut;
	InputMethodManager imm;
	
	private int loc_id;
	private int acc_loc_id;
	private int userId;
	private int malemark = -1;
	int positionMark; 
	private String myNickname;
	private String myemail;
	private String birthday ;
	private String changedBirthday;
	private String mybreast;
	private String myInterest;
	private String mylocation;
	private String newpwd;
	private String myavatar;
	private AsynImageLoader imageLoader = new AsynImageLoader();
	private ListView mListView;
	private UserSettingAdapter mAdapter;
	Map<String, Object> item = new HashMap<String, Object>();
	private String[] mStyleNames ;
	private String[] mStyleDetail;
	private String avatarUrl;
	private Button saveBtn;
	private String photoPath;
	private String photoName;
	private ImageView avatarView;
	private Bitmap bitmap;
	int status = -1;
	private ProgressDialog pd;
	private final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Thread thread;
			switch (msg.what) {
			case USERINFO_LOADING:
				
				thread = new Thread(new Runnable() {
					public void run() {
						initUserDetailMessage();
						
						mHandler.sendEmptyMessage(USERINFO_LONDED);
					}
				});
				thread.start();
				break;

			case USERINFO_LONDED:
				mAdapter = new UserSettingAdapter();
				mListView.setAdapter(mAdapter);
				bindItemClick();
				break;
			case UPLOADING_INFO:
				
				pd = ProgressDialog.show(UserSettingActivity.this, "", getString(R.string.dataupdate), true, true);
				thread = new Thread(new Runnable() {
					
					public void run() {
						// TODO Auto-generated method stub
						Looper.prepare();
						try {
							if(photoPath != null)
								uploadFile();
						} catch (ClientProtocolException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						
						status = initSaveSetting();
						
						mHandler.sendEmptyMessage(UPLOADED_INFO);
						Looper.loop();
					
						
					}
				});
				thread.start();
				
				break;
			case UPLOADED_INFO:
				if(pd != null && pd.isShowing()){
					pd.cancel();
				}
				if (status == 1) {
					if(bitmap != null){
						UserInfoEntity uEntity = UserInfoDAO.getUserInfo(UserSettingActivity.this);
						uEntity.setAvatarUrl(photoName);
						uEntity.setUserAvatar(bitmap);
						UserInfoDAO.setUserInfo(UserSettingActivity.this, uEntity);
						
					}
					Toast.makeText(UserSettingActivity.this, getString(R.string.setting_set_success), Toast.LENGTH_LONG).show();
					
					NavUtils.navigateUpFromSameTask(UserSettingActivity.this);
				}else{
					Toast.makeText(UserSettingActivity.this, getString(R.string.setting_set_fail), Toast.LENGTH_LONG).show();
					
				}
				break;
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_userdetail);
		imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		
		initInputFields();

		initAnimation();
		mListView = (ListView)findViewById(R.id.settingListView);
		userId = UserInfoDAO.getUserInfo(UserSettingActivity.this).getUserId();
		
		initData();
		initSettingInputFieldButton();
		saveBtn = (Button)findViewById(R.id.settings_save_button);
		saveBtn.setOnClickListener(clickListener);
		mHandler.sendEmptyMessage(USERINFO_LOADING);
	}
	
	public void initData(){
		mStyleNames = new String[]{
				getString(R.string.setting_account),  getString(R.string.setting_password),  getString(R.string.setting_avater),
				getString(R.string.setting_email),    getString(R.string.setting_sex),       getString(R.string.settign_born),
				getString(R.string.setting_nickname), getString(R.string.setting_myhart),    getString(R.string.setting_myhobby),
				getString(R.string.setting_city),     getString(R.string.setting_logout),    getString(R.string.setting_tips),
				getString(R.string.setting_version)
		};
		
		mStyleDetail = new String[mStyleNames.length];
	}
	
	private void initInputFields() {
		mInputFieldOneLine = findViewById(R.id.inputfield_oneline);
		mInputFieldOneLine.setVisibility(View.GONE);
		mInputFieldOneLineTextView = (TextView) findViewById(R.id.inputfield_title);
		mInputFieldOneLineEditText = (EditText) findViewById(R.id.inputfield_edittext);

		mInputFieldMoreLines = findViewById(R.id.inputfield_moreline);
		mInputFieldMoreLines.setVisibility(View.GONE);
		mInputFieldMoreLinesEditText = (EditText) findViewById(R.id.inputfieldmoreline_edittext);
	}
	
	private void initAnimation() {
		mAnimIn = AnimationUtils.loadAnimation(this, R.anim.anim_register_in);
		mAnimOut = AnimationUtils.loadAnimation(this, R.anim.anim_register_out);
	}
	
	private OnClickListener clickListener = new OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.settings_save_button:
				mHandler.sendEmptyMessage(UPLOADING_INFO);
				break;

			default:
				break;
			}
		}
	};
	
	public int initSaveSetting(){
		JSONObject request = new JSONObject();
		int status = -1;
		try {
			request.put(Constant.PARAM_USERID, userId);
			request.put("interest", myInterest);
			
			request.put("avatar", myavatar);
			request.put("breast", mybreast);
			request.put("nickname", myNickname);
			request.put("loc_id", loc_id);
			request.put("acc_loc_id", acc_loc_id);
			request.put("email", myemail);
			request.put("birthday", changedBirthday);
			request.put("sex", malemark);
			JSONResponse jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_SAVESETTING, request);
			if (jsonResponse.getStatus() != Status.ERR) {
				JSONObject data = jsonResponse.getData();
				status = data.getInt(Constant.RESP_STATUS);
				
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return status;
	}
	private void initSettingInputFieldButton() {
		// one line
		Button inputCancelButton = (Button) findViewById(R.id.inputfield_cancelbutton);
		inputCancelButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				hideOneLineInputField();
			}
		});

		Button inputOkButton = (Button) findViewById(R.id.inputfield_okbutton);
		inputOkButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				switch (positionMark) {
				case 1:
					String oldpwd = mInputFieldOneLineEditText.getText().toString().trim();
					hideOneLineInputField();
					if(oldpwd.equals(UserInfoDAO.getUserInfo(UserSettingActivity.this).getUserPassword())){
						mInputFieldOneLineTextView.setText(getString(R.string.setting_new_password));
						showOneLineInputField();
						mInputFieldOneLineEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
						positionMark = 111;
					}else{
						Toast.makeText(UserSettingActivity.this, getString(R.string.setting_wrong_oldpwd),Toast.LENGTH_LONG).show();
					}
					break;
				case 3:
					String newEmail = mInputFieldOneLineEditText.getText().toString().trim();
					if(!newEmail.equals("")&& ViewsUtil.validateEmail(newEmail)){
						myemail = newEmail;
						hideOneLineInputField();
					}else{
						Toast.makeText(UserSettingActivity.this, getString(R.string.setting_error_email), Toast.LENGTH_LONG).show();
						
					}
					break;
				case 6:
					String newNickname = mInputFieldOneLineEditText.getText().toString().trim();
					if(newNickname.equals("")){
						Toast.makeText(UserSettingActivity.this, getString(R.string.setting_error_nickname), Toast.LENGTH_LONG).show();
					}else{
						hideOneLineInputField();
						myNickname = newNickname;
					}
					break;
				case 111:
					String content = mInputFieldOneLineEditText.getText().toString().trim();
					
					if(content.equals("")|| content.length()>23 || content.length() <5){
						Toast.makeText(UserSettingActivity.this, getString(R.string.setting_error_newpwd), Toast.LENGTH_LONG).show();
						
					}else{
						
						Pattern pattern = Pattern.compile("^[a-zA-Z0-9][a-zA-Z0-9_]{4,22}$");
						if(pattern.matcher(content).matches()){
							newpwd = content;	
							hideOneLineInputField();
							Log.d("newpwd", newpwd);
						}
						else{
							Toast.makeText(UserSettingActivity.this, getString(R.string.setting_error_newpwd), Toast.LENGTH_LONG).show();
						}
					}
					break;
				default:
					break;
				}
			}
		});
		// more liness
		Button inputMoreLineCancelButton = (Button) findViewById(R.id.inputfieldmoreline_cancelbutton);
		inputMoreLineCancelButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				hideMoreLinesInputField();
			}
		});

		Button inputMoreLineOkButton = (Button) findViewById(R.id.inputfieldmoreline_okbutton);
		inputMoreLineOkButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				String content = mInputFieldMoreLinesEditText.getText().toString().trim();
				if(!content.equals("")){
					hideMoreLinesInputField();
				}
				switch (positionMark) {
				case 7:
					mybreast = content;
					mStyleDetail[7] = mybreast;
					mAdapter.notifyDataSetChanged();
					break;
				case 8:
					myInterest = content;
					mStyleDetail[8] = myInterest;
					mAdapter.notifyDataSetChanged();
					break;
				default:
					break;
				}
			}
		});
	}
	
	private void showOneLineInputField() {
		mInputFieldOneLine.startAnimation(mAnimIn);
		mInputFieldOneLine.setVisibility(View.VISIBLE);
		mInputFieldOneLineEditText.requestFocus();
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
	}

	private void showMoreLinesInputField() {
		mInputFieldMoreLines.startAnimation(mAnimIn);
		mInputFieldMoreLines.setVisibility(View.VISIBLE);
		mInputFieldMoreLinesEditText.requestFocus();
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
	}
	
	private void hideOneLineInputField() {
		mInputFieldOneLineEditText.setText("");
		mInputFieldOneLineEditText.setInputType(InputType.TYPE_NULL);
		mInputFieldOneLineEditText.clearFocus();
		mInputFieldOneLine.startAnimation(mAnimOut);
		mInputFieldOneLine.setVisibility(View.GONE);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
	}

	private void hideMoreLinesInputField() {
		mInputFieldMoreLinesEditText.setText("");
		mInputFieldMoreLinesEditText.clearFocus();
		mInputFieldMoreLines.startAnimation(mAnimOut);
		mInputFieldMoreLines.setVisibility(View.GONE);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
	}

	
	public void bindItemClick(){
		mListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				positionMark = position;
				switch (position) {
				case 1:
					mInputFieldOneLineTextView.setText(getString(R.string.setting_old_password));
					mInputFieldOneLineEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
					mInputFieldOneLineTextView.setTextSize(15);
					showOneLineInputField();
					
					break;
				case 2:
					Intent avatarIntent = new Intent(UserSettingActivity.this,UserAvatarSelectActivity.class);
					startActivityForResult(avatarIntent, Constant.SETTING_AVATAR_CODE);
					break;
				case 3:
					mInputFieldOneLineTextView.setText(getString(R.string.setting_new_email));
					mInputFieldOneLineTextView.setTextSize(15);
					showOneLineInputField();
					break;
				case 5:
					Intent intent = new Intent(UserSettingActivity.this, UserSettingBirthdayActivity.class);
					intent.putExtra("birthday", birthday);
					startActivityForResult(intent, Constant.SETTING_BIRTHDAY_CODE);
					break;
				case 6:
					mInputFieldOneLineTextView.setText(getString(R.string.setting_old_nickname));
					showOneLineInputField();
					mInputFieldOneLineTextView.setTextSize(15);
					
					break;
				case 7:
				case 8:
					showMoreLinesInputField();
					break;
				case 9:
					Intent locIntent = new Intent(UserSettingActivity.this, UserSettingLocationActivity.class);
					startActivityForResult(locIntent, Constant.SETTING_LOCATION_CODE);
					break;
				case 10:
					 UserInfoDAO.clearUserInfo(getApplicationContext());
                     ViewsUtil.showErrorTip(getApplicationContext(), R.string.logout_success_tip);
                     Intent logoutIntent = new Intent(getApplicationContext(), FirstLoginActivity.class);
                     startActivity(logoutIntent);
                     UserSettingActivity.this.finish();
                     break;
				case 11:
					Intent jcIntent = new Intent(UserSettingActivity.this,UserSettingTutorialsActivity.class);
					startActivity(jcIntent);
					break;
				default:
					break;
				}
			}
		});
	}
	
	public void uploadFile() throws ClientProtocolException, IOException{
		File file = null;
		
		file = new File(photoPath);
		
		 try {
//			 	URL url = new URL("http://192.168.3.100:8088/phpProject/push/android_upload.php?filename="+photoName);
	            URL url = new URL(Constant.URL_USER_AVAR +"?filename="+photoName);
	            HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
	            httpUrlConnection.setDoOutput(true);
	            httpUrlConnection.setDoInput(true);
	            httpUrlConnection.setUseCaches(false);
	            httpUrlConnection.setRequestMethod("POST");
	            OutputStream os = httpUrlConnection.getOutputStream();
	            Thread.sleep(100);
	            BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file));

	            int bufSize = 0;
	            byte[] buffer = new byte[1024];
	            while ((bufSize = fis.read(buffer)) != -1) {
	                os.write(buffer, 0, bufSize);
	            }
	            fis.close();

	            String responMsg = httpUrlConnection.getResponseMessage();
	        } catch (Exception e) {
	           e.printStackTrace();
	        }
	
	}
	
	public void initUserDetailMessage(){
		JSONObject request = new JSONObject();
		try {
			request.put(Constant.PARAM_USERID, userId);
			JSONResponse jsonResponse = JSONUtil.postRequest(Constant.REQ_WAY_GETUSERDETAILINFO, request);
			if (jsonResponse.getStatus() != Status.ERR) {
				JSONObject data = jsonResponse.getData();
				int status = -1;
				status = data.getInt(Constant.RESP_STATUS);
				if (status == Constant.RESP_STATUS_SUCCESS) {
					initUserInfoItemFromJSONObject(data.getJSONArray(Constant.RESP_USERDATA ));
				} else {
					ViewsUtil.showErrorTip(UserSettingActivity.this, R.string.global_error_networkerr);
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	
	private void initUserInfoItemFromJSONObject(JSONArray jsonArray) throws JSONException {
		Gson gson = new Gson();
		UserInfoDetailEntity userEntity = gson.fromJson(jsonArray.getJSONObject(0).toString(), UserInfoDetailEntity.class);
		myNickname = userEntity.getUd_nickname();
		Log.d("UserSettingNickname", myNickname);
		mStyleDetail[0] = userEntity.getUser_account();
		mStyleDetail[1] = getString(R.string.setting_password_detail);
		mStyleDetail[2] = userEntity.getUser_avatar();
		myavatar = userEntity.getUser_avatar();
		
		myemail = userEntity.getUd_email();
		if(myemail == null || myemail.equals("")){
			mStyleDetail[3] = getString(R.string.setting_unset);
		}else{
			mStyleDetail[3] = userEntity.getUd_email();
		}
		
		malemark = userEntity.getUd_sex();
		if(malemark == 0){
			mStyleDetail[4] = getString(R.string.setting_sex_detail) + getString(R.string.setting_unset);
		}
		else if(malemark == 1){
			mStyleDetail[4] = getString(R.string.setting_sex_detail) + getString(R.string.setting_sex_man);
		}else if(malemark == 2){
			mStyleDetail[4] = getString(R.string.setting_sex_detail) + getString(R.string.setting_sex_woman);
		}else {
			mStyleDetail[4] = getString(R.string.setting_sex_detail) + getString(R.string.setting_secret);
		}
		
		birthday = userEntity.getUd_birthday();
		if(birthday == null || birthday .equals("")){
			mStyleDetail[5] = getString(R.string.setting_unset);
		}
		else{
			String[]detailDates = birthday.split(" ");
			mStyleDetail[5] = String.format(getString(R.string.setting_birthday_text), detailDates[2],detailDates[0],detailDates[1]);
		}
		
		
		mStyleDetail[6] = userEntity.getUd_nickname();
		
		mybreast = userEntity.getUd_breast();
		if(mybreast == null || mybreast.equals("")){
			mStyleDetail[7] = getString(R.string.setting_unset);
		}else {
			mStyleDetail[7] = mybreast;
		}
		
		myInterest = userEntity.getUd_interest();
		if(myInterest == null || myInterest.equals("")){
			mStyleDetail[8] = getString(R.string.setting_unset);
		}else{
			mStyleDetail[8] = myInterest;
		}
		
		mylocation = userEntity.getLoc_name();
		if(mylocation == null || mylocation.equals("")){
			mStyleDetail[9] = getString(R.string.setting_unset);
		}else{
			mStyleDetail[9] = mylocation;
		}
		mStyleDetail[10] = getString(R.string.setting_logout_detail);
		
		mStyleDetail[11] = getString(R.string.setting_detail_tips);
		mStyleDetail[12] = getString(R.string.setting_detail_verison);
		avatarUrl = userEntity.getUser_avatar();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if(resultCode == RESULT_OK){
			switch (requestCode) {
			case Constant.SETTING_BIRTHDAY_CODE:
				Bundle bundle = data.getExtras();
				changedBirthday = bundle.getString("birthday");
				String[]strs = changedBirthday.split("-");
				birthday = strs[1]+" "+strs[2]+" "+strs[0];
				Log.d("UserSettingIntentBirthday", birthday);
				mStyleDetail[5] = String.format(getString(R.string.setting_birthday_text), strs[0],strs[1],strs[2]);
				mAdapter.notifyDataSetChanged();
				break;
			case Constant.SETTING_LOCATION_CODE:
				Bundle locBundle = data.getExtras();
				String locName = locBundle.getString("locName");
				mStyleDetail[9] = locName;
				loc_id = locBundle.getInt("loc_id");
				acc_loc_id = locBundle.getInt("acc_loc_id");
				mAdapter.notifyDataSetChanged();
				break;
			case Constant.SETTING_AVATAR_CODE:
				Bundle avatarBundle = data.getExtras();
				photoPath = avatarBundle.getString("photoPath");
				photoName = avatarBundle.getString("photoName");
				if(photoPath != null){
					if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
						Toast.makeText(UserSettingActivity.this, getString(R.string.sdcard_notin), Toast.LENGTH_LONG).show();
					}else{
						final BitmapFactory.Options options = new BitmapFactory.Options();
						InputStream is = null;
						try {
							is = new FileInputStream(photoPath);
							
						} catch (FileNotFoundException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

				        options.inSampleSize = 2;

				        options.inJustDecodeBounds = false;
				        bitmap = BitmapFactory.decodeStream(is, null, options);
						
						avatarView.setImageBitmap(bitmap);
					}
				}else{
					int bmpid = avatarBundle.getInt("photoResourceId");
					bitmap = BitmapFactory.decodeResource(getResources(), bmpid);
					avatarView.setImageBitmap(bitmap);
				}
				myavatar = photoName;
				break;
			default:
				break;
			}
		}
	}
	
	
	public class UserSettingAdapter extends BaseAdapter{

		public int getCount() {
			// TODO Auto-generated method stub
			return mStyleNames.length;
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}
		
	
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if(position == 2){
				convertView = LayoutInflater.from(UserSettingActivity.this).inflate(R.layout.view_userdetail_avatar_item, null);	
				TextView styleView = (TextView)convertView.findViewById(R.id.userinfo_style);
				TextView msgView = (TextView)convertView.findViewById(R.id.userinfo_style_detail);
				avatarView = (ImageView)convertView.findViewById(R.id.userinfo_style_avatar);
				
				styleView.setText(mStyleNames[position]);
				msgView.setText(mStyleDetail[position]);
				if(avatarView != null){
					imageLoader.showImageAsyn(avatarView, Constant.URL_USER_SMALL+avatarUrl, R.drawable.glm_white);
				}
				
			}
			else if(position == 4){
				convertView = LayoutInflater.from(UserSettingActivity.this).inflate(R.layout.view_userdetail_gender_item, null);
				TextView styleView = (TextView)convertView.findViewById(R.id.userinfo_style);
				final TextView msgView = (TextView)convertView.findViewById(R.id.userinfo_style_detail);
				styleView.setText(mStyleNames[position]);
				msgView.setText(mStyleDetail[position]);
				Button maleBtn = (Button)convertView.findViewById(R.id.male_button);
				maleBtn.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						// TODO Auto-generated method stub
						malemark = 1;
						msgView.setText(getResources().getString(R.string.setting_sex_detail)+getString(R.string.setting_sex_man));
						
					}
				});
				
				Button femaleBtn = (Button)convertView.findViewById(R.id.female_button);
				femaleBtn.setOnClickListener(new OnClickListener() {
					
					public void onClick(View v) {
						// TODO Auto-generated method stub
						malemark = 2;
						msgView.setText(getResources().getString(R.string.setting_sex_detail)+getString(R.string.setting_sex_woman));
						
					}
				});
			}
			else {
				convertView = LayoutInflater.from(UserSettingActivity.this).inflate(R.layout.view_userdetail_normal_item, null);
				TextView styleView = (TextView)convertView.findViewById(R.id.userinfo_style);
				TextView msgView = (TextView)convertView.findViewById(R.id.userinfo_style_detail);
				styleView.setText(mStyleNames[position]);
				msgView.setText(mStyleDetail[position]);
				
			}
			return convertView;
		}
		
	}

	public void goBackButton_OnClick(View v) {
		NavUtils.navigateUpFromSameTask(this);
	}
}
